/**
 * 我的积分页面
 * author:zhushy
 * date:2017年3月10日13:30:42
 */
;
define(function(require, exports, module) {
	var wechat = require("wechat");
	var template = require("template");
	template.helper('formatAmount', function(point) {
		if(point) {
			var point = Number(point);
			if(point != NaN) {
				return point;
			} else {
				return 0;
			}
		} else {
			return 0;
		}
	});
	var account = {
		data: {},
		currentPage: 1,
        pageSize: 20,
        loadedAllFlag:false,
		/**
		 * bindEvent 页面事件
		 */
		bindEvent: function() {
			//返回
			$("#return_account").unbind().on("click", function() {
				window.location.href = "#home";
			});
		},
		/**
		 * 进行页面路由监听配置
		 * 
		 */
		bindRout: function() {
			//公共页面监听路由
			Path.map("#home").to(function() {
				//返回首页
				common.loadPage({
					pageUrl: "html/index.html",
					reload: false,
					from: "left"
				});
			});
			Path.listen();
		},
		/**
		 * 页面数据加载
		 */
		loadData: function() {
			var _this = this;
			var param = {
				"openId": window.localStorage.getItem("openId"),
				"appId": window.localStorage.getItem("appId"),
				"currentPage": _this.currentPage,
        			"pageSize": _this.pageSize,
			};
			common.loadData({
				url: "/wxmember/service/getAccountList",
				param: param,
				successCallBack: _this.getAccountListCallBack, //回调函数
				dataSource: "getAccountList",
				loadAll:_this.loadedAllFlag //标识是不加载全部
			});
			

		},
		pullUpActionCallBack:function(){
			account.currentPage +=1;
			account.loadData();
		},
		pullDownActionCallBack:function(){
			account.currentPage = 1;
			$("#main_account").html("");
			account.loadedAllFlag=false;
			account.loadData();
		},
		//callback function
		getAccountListCallBack: function(json) {
			console.log("调用后台ajax返回值为：" + JSON.stringify(json));
			$("#showNodata_account").css("min-height",$(window).height()-$(".header").height()-$(".jf_banner").height());
			if(json.status == "0") {					
				if(json.data && json.data.list &&json.data.list.length>0){
					if(json.data.totalAmount=="0"){
						$("#totalAmount_account").text("¥0.00");	
					}else{
						$("#totalAmount_account").text("¥"+json.data.totalAmount);	
					}
					
					var html2 = template('accountTemplate', json);
					$("#main_account").append(html2);
					//如果加载全部
					if(account.currentPage > json.data.totalPage){
						//两个部分共同控制加载全部
						common.loadedAll();
						account.loadedAllFlag = true;
					}
				}else{
					if(account.currentPage==1){
						common.show_nodata("当前没有余额变动信息","showNodata_account");
					}else{
						//两个部分共同控制加载全部
						common.loadedAll();
						account.loadedAllFlag = true;
					}
				}
				
				//iscroll滚动加入
				var iscroll = require("iscroll");
				common.initIscroll({
					container: "iscrollContent_account",
					showScrollBar: false,
					pullUpActionCallBack:account.pullUpActionCallBack,
					pullDownActionCallBack:account.pullDownActionCallBack,
					loadAll:account.loadedAllFlag
				});
			} else {
				if(account.currentPage == 1){
					var window_height = document.documentElement.clientHeight;
					$("#showNodata_account").height(window_height-$(".header").height()-$(".jf_banner").height());
					common.show_nodata("当前没有余额变动信息","showNodata_account");
				}
				common.showTip({
					content:json.message
				});
			}

		},
		/**
		 * each page init mothed
		 */
		initialize: function() {
			//初始化属性
			this.currentPage = 1;
			this.loadedAllFlag = false;
			this.currentPage = 1;
			this.loadedAllFlag = false;
            if(common.isWeiXin()){
            	$("#jfbanner_account").css("margin-top","0rem");
            	$("#iscrollContent_account").css("top","5.6rem");
            }
            
            //动态加载数据
			this.loadData();
			//事件绑定
			this.bindEvent();
			//路由绑定
			this.bindRout();
		}
	};

	module.exports = account;
});